//
//  CnEvent.swift
//  foodApp1
//
//  Created by Jenny Shi on 2018/1/5.
//  Copyright © 2018年 Apple Inc. All rights reserved.
//

import UIKit
import os.log

class CuEvent: NSObject, NSCoding {

    struct PropertyKey {
        static let id = "_id"
        static let owner = "owner"
        static let calendarType = "calendarType"
        static let eventType = "eventType"
        static let eventTitle = "eventTitle"
        static let description = "description"
        static let date = "date"
        static let startTime = "startTime"
        static let endTime = "endTime"
        static let timeSensibility = "timeSensibility"
        static let guestStatus = "guestStatus"
        static let friendGo = "friendGo"
        static let photo = "photo"
        static let loginOwnerGo = "loginOwnerGo"
    }

    //MARK: Properties
    
    var eid: String
    var eowner: String
    var ecalendarType: String
    var eeventType: String
    var eeventTitle: String
    var edescription: String
    var edate: String
    var estartTime: String
    var eendTime: String
    var etimeSensibility: String
    var eguestStatus: String
    var efriendGo: String
    var ephoto: UIImage?
    var eloginOwnerGo : String

    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("cuEventArr")
    
    //MARK: Initialization
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(eid, forKey: PropertyKey.id)
        aCoder.encode(eowner, forKey: PropertyKey.owner)
        aCoder.encode(ecalendarType, forKey: PropertyKey.calendarType)
        aCoder.encode(eeventType, forKey: PropertyKey.eventType)
        aCoder.encode(eeventTitle, forKey: PropertyKey.eventTitle)
        aCoder.encode(edescription, forKey: PropertyKey.description)
        aCoder.encode(edate, forKey: PropertyKey.date)
        aCoder.encode(estartTime, forKey: PropertyKey.startTime)
        aCoder.encode(eendTime, forKey: PropertyKey.endTime)
        aCoder.encode(etimeSensibility, forKey: PropertyKey.timeSensibility)
        aCoder.encode(eguestStatus, forKey: PropertyKey.guestStatus)
        aCoder.encode(efriendGo, forKey: PropertyKey.friendGo)
        aCoder.encode(ephoto, forKey: PropertyKey.photo)
        aCoder.encode(eloginOwnerGo, forKey: PropertyKey.loginOwnerGo)
    }

    required convenience init?(coder aDecoder: NSCoder) {
        // The title is required. If we cannot decode a name string, the initializer should fail.
        guard let eventTitle = aDecoder.decodeObject(forKey: PropertyKey.eventTitle) as? String else {
            os_log("Unable to decode the title for a CyEvent object.", log: OSLog.default, type: .debug)
            return nil
            
        }
        
        // Because photo is an optional property of Meal, just use conditional cast.
        let id = aDecoder.decodeObject(forKey: PropertyKey.id) as? String
        let owner = aDecoder.decodeObject(forKey: PropertyKey.owner) as? String
        let calendarType = aDecoder.decodeObject(forKey: PropertyKey.calendarType) as? String
        let eventType = aDecoder.decodeObject(forKey: PropertyKey.eventType) as? String
        let description = aDecoder.decodeObject(forKey: PropertyKey.description) as? String
        let date = aDecoder.decodeObject(forKey: PropertyKey.date) as? String
        let startTime = aDecoder.decodeObject(forKey: PropertyKey.startTime) as? String
        let endTime = aDecoder.decodeObject(forKey: PropertyKey.endTime) as? String
        let timeSensibility = aDecoder.decodeObject(forKey: PropertyKey.timeSensibility) as? String
        let guestStatus = aDecoder.decodeObject(forKey: PropertyKey.guestStatus) as? String
        let friendGo = aDecoder.decodeObject(forKey: PropertyKey.friendGo) as? String
        let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
        let loginOwnerGo = aDecoder.decodeObject(forKey: PropertyKey.loginOwnerGo) as? String

        // Must call designated initializer.
        self.init(id : id!, owner : owner!, calendarType : calendarType!, eventType : eventType!, eventTitle : eventTitle, description : description!, date : date!, startTime : startTime!, endTime : endTime!, timeSensibility : timeSensibility!, guestStatus : guestStatus!, friendGo : friendGo!,  photo : photo, loginOwnerGo : loginOwnerGo!)

    }
    
    init?(id : String, owner : String, calendarType : String, eventType : String, eventTitle : String, description : String, date : String, startTime : String, endTime : String, timeSensibility : String, guestStatus : String, friendGo : String, photo : UIImage?, loginOwnerGo: String) {

        // Initialization should fail if there is no name or if the rating is negative.
//        if eventTitle.isEmpty || description.isEmpty  {
//            return nil
//        }
        
        // Initialize stored properties.
        self.eid = id
        self.eowner = owner
        self.ecalendarType = calendarType
        self.eeventType = eventType
        self.eeventTitle = eventTitle
        self.edescription = description
        self.edate = date
        self.estartTime = startTime
        self.eendTime = endTime
        self.etimeSensibility = timeSensibility
        self.eguestStatus = guestStatus
        self.efriendGo = friendGo
        self.ephoto = photo
        self.eloginOwnerGo = loginOwnerGo

    }

}
